package com.refid.api.util;

import java.io.Closeable;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

public class BuleUtil {

	/**
	 * trim 后的结果为空或空字符串时null,去前后空格
	 * 
	 * @param str
	 * @return
	 */
	public static String trimTONull(String str) {
		if (str == null || str.trim().length() == 0)
			return null;
		return str.trim();
	}

	/**
	 * 线程休眠
	 * 
	 * @param millis 毫秒数
	 */
	public static void sleep(long millis) {
		try {
			Thread.sleep(millis);
		} catch (InterruptedException e) {
		}
	}

	/**
	 * 关闭流等信息
	 * 
	 * @param closeable
	 */
	public static void close(Closeable closeable) {
		try {
			if (closeable != null) {
				closeable.close();
			}
		} catch (IOException e) {

		}
	}

	public static String toString(List<String> list) {
		StringBuilder sb = new StringBuilder();
		for (int i = 0; list != null && i < list.size(); i++) {
			if (sb.length() > 0)
				sb.append(",");
			sb.append(list.get(i));
		}
		return "[" + sb.toString() + "]";
	}

	public static String toString(Set<String> set) {
		StringBuilder sb = new StringBuilder();
		if (set == null || set.size() == 0)
			return sb.toString();
		Iterator<String> it = set.iterator();
		while (it.hasNext()) {
			if (sb.length() > 0)
				sb.append(",");
			sb.append(it.next());
		}
		return sb.toString();
	}

	/**
	 * set1 == set2
	 * 
	 * @param set1 参数集1
	 * @param set2 参数集2
	 * @return
	 */
	public static boolean compare(Set<String> set1, Set<String> set2) {
		if (set1 != null && set2 != null) {
			return set1.containsAll(set2) && set2.containsAll(set1);
		} else if (set1 != null && set2 == null) {
			return false;
		} else if (set1 == null && set2 != null) {
			return true;
		}
		return false;
	}

	/**
	 * set1>=set2
	 * 
	 * @param set1 参数集1
	 * @param set2 参数集2
	 * @return
	 */
	public static boolean compareEqOrBig(Set<String> set1, Set<String> set2) {
		if (set1 != null && set2 != null) {
			return set1.containsAll(set2);
		} else if (set1 != null && set1.size() == 0 && set2 == null) {
			return true;
		} else if (set1 != null && set2 == null) {
			return true;
		} else if (set1 == null && set2 != null && set2.size() == 0) {
			return true;
		} else if (set1 == null && set2 != null) {
			return false;
		}
		return false;
	}

}
